package com.chexiehui.jc.adapter;

import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.chexiehui.jc.R;
import com.chexiehui.jc.utils.AppCommonUtil;
import com.chexiehui.jc.utils.GlideUtil;
import com.chexiehui.jc.utils.NoDoubleClickListener;

import java.util.List;

public class SeriesBannerAdapter extends PagerAdapter {

    private List<String> dataList;
    private OnItemClickListener itemClickListener;

    public SeriesBannerAdapter(List<String> imgDataList, OnItemClickListener itemClickListener) {
        this.dataList = imgDataList;
        this.itemClickListener = itemClickListener;
    }

    @Override
    public int getCount() {
        return dataList != null ? dataList.size() : 0;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = LayoutInflater.from(container.getContext())
                .inflate(R.layout.item_series_banner, null);
        ImageView iv_item_image = view.findViewById(R.id.iv_item_image);
        ImageView iv_item_play = view.findViewById(R.id.iv_item_play);
        if(AppCommonUtil.isVideo(dataList.get(position))){
            iv_item_play.setVisibility(View.VISIBLE);
        }else{
            iv_item_play.setVisibility(View.GONE);
        }
        if(itemClickListener != null){
            iv_item_image.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View view) {
                    itemClickListener.onItemClick(dataList.get(position), AppCommonUtil.isVideo(dataList.get(position)));
                }
            });
        }
        GlideUtil.loadImageViewCrop(container.getContext(), dataList.get(position), iv_item_image, R.mipmap.ic_logo_2_b, R.mipmap.ic_logo_2_b);
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View view = (View) object;
        container.removeView(view);
    }

    public List<String> getDataList() {
        return dataList;
    }

    public interface OnItemClickListener{
        void onItemClick(String url, boolean isVideo);
    }
}
